package com.gbb.common.tools;

import android.util.Log;

import com.gbb.common.log.LogUtils;

import java.util.List;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

/**
 * Fragment操作工具类
 *
 * @author gaobingbing on 2016/8/20.
 */
public class FragmentHelper {

    private final FragmentManager fragmentManager;

    private FragmentHelper(FragmentManager fragmentManager) {
        this.fragmentManager = fragmentManager;
    }

    public static FragmentHelper getNewInstance(FragmentManager fragmentManager) {
        return new FragmentHelper(fragmentManager);
    }

    /**
     * 將Fragment添加到栈中
     *
     * @param sourdID 资源ID，即需要添加到哪个资源里面
     * @param newFragment 新Fragment
     * @param isAddToStack 是否添加到Fragment栈里面
     */
    public synchronized void addFragmentToStack(int sourdID,Fragment newFragment,boolean isAddToStack) {
        if(newFragment != null) {
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            transaction.add(sourdID,newFragment,newFragment.getClass().getSimpleName());
            if(isAddToStack) {
                transaction.addToBackStack(newFragment.getClass().getSimpleName());
            }
            transaction.commitAllowingStateLoss();
        }
    }

    /**
     * 替换Fragment
     *
     * @param sourdID 资源ID，即需要添加到哪个资源里面
     * @param newFragment 新Fragment
     */
    public synchronized void replaceFragmentToStack(int sourdID,Fragment newFragment) {
        if(newFragment != null) {
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            transaction.replace(sourdID,newFragment,newFragment.getClass().getSimpleName());
            transaction.commitAllowingStateLoss();
        }
    }

    /**
     * 切换Fragment的显示/隐藏
     *
     * @param mFragment {@link Fragment}
     */
    public synchronized void toggleFragment(Fragment mFragment) {
        if(mFragment != null) {
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            if(mFragment.isHidden()) {
                transaction.show(mFragment);
            } else {
                transaction.hide(mFragment);
            }
            transaction.commitAllowingStateLoss();
        }
    }

    /**
     * 将当前的Fragment从FragmentStack中推出
     */
    public synchronized void popTopFragment() {
        if(fragmentManager.getBackStackEntryCount() > 0) {
            fragmentManager.popBackStackImmediate();
        }
    }

    /**
     * 打印放在BackStack中的所有Fragment
     */
    public synchronized void printAllBackStackFragment() {
        for(int i = 0; i < fragmentManager.getBackStackEntryCount(); i++) {
            FragmentManager.BackStackEntry backStackEntry = fragmentManager.getBackStackEntryAt(i);
            Log.e("Fragment",fragmentManager.findFragmentByTag(backStackEntry.getName()) + "");
        }
    }

    /**
     * 打印当前FragmentManager中所有的Fragment
     */
    public synchronized void printAllFragment() {
        List<Fragment> fragmentList = fragmentManager.getFragments();
        for(Fragment fragment : fragmentList) {
            if(fragment != null) {
                LogUtils.i("fragment.tag",fragment.getTag());
            }
        }
    }

    /**
     * @return 获得当前FragmentManager中位于顶栈的Fragment
     */
    public synchronized Fragment getCurFragment() {
        List<Fragment> fragmentList = fragmentManager.getFragments();
        if(fragmentList.size() > 0) {
            return fragmentList.get(fragmentList.size() - 1);
        }
        return null;
    }

    /**
     * @return 获得BackStack中位于顶部的Fragment
     */
    public synchronized Fragment getTopFragment() {
        int backStackEntryCount = fragmentManager.getBackStackEntryCount();
        if(backStackEntryCount > 0) {
            FragmentManager.BackStackEntry backStackEntry = fragmentManager.getBackStackEntryAt(backStackEntryCount - 1);
            return fragmentManager.findFragmentByTag(backStackEntry.getName());
        }
        return null;
    }

    /**
     * 通过Tag查找Fragment
     *
     * @param fragmentTag Fragment的Tag
     * @return Fragment或者null
     */
    public synchronized Fragment getFragmentByTag(String fragmentTag) {
        return fragmentManager.findFragmentByTag(fragmentTag);
    }

}
